﻿using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using ARC.AliYun.SMSService.Interfaces;
using ARC.AliYun.SMSService.Models;
using ARC.AliYun.SMSService.Models.DigitalSmsModels;
using System.Collections.Generic;
using System.Text.Json;


namespace ARC.AliYun.SMSService.Service.DigitalSmsService
{
    public class DigitalSmsService : BaseAliyun, IDigitalSms
    {
        private readonly CommonRequest _request;
        public DigitalSmsService(string accessKeyId, string secret)
        {
            _client = new DefaultAcsClient(DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, secret));//初始化阿里云控制客户端
            _request = CreateCommonRequest();
            _request.Method = MethodType.POST;
            _request.Domain = "dysmsapi.aliyuncs.com";
            _request.Version = "2017-05-25";
        }

        public AddAndDelDigitalTemResponse AddDigitalTemplate(string templateName, List<DigitalSmsAddTemFileArray> files, string remark)
        {
            _request.Action = "CreateDigitalSmsTemplate";
            _request.AddQueryParameters("TemplateName", templateName);//模板名称
            for (int i = 0; i < files.Count; i++)
            {
                _request.AddQueryParameters($"TemplateContents.{1}.FileSuffix", files[i].FileSuffix);//文件后缀名
                _request.AddQueryParameters($"TemplateContents.{1}.FileName", files[i].Filename);//文件名
                _request.AddQueryParameters($"TemplateContents.{1}.FileSize", files[i].FileSize);//文件大小
                _request.AddQueryParameters($"TemplateContents.{1}.FileContents", files[i].FileContents);//文件内容
            }
            _request.AddQueryParameters("Remark", remark);
            return Json<AddAndDelDigitalTemResponse>(_request);
        }

        public AddAndDelDigitalTemResponse DeleDigitalTemplate(string templateCode)
        {
            _request.Action = "DeleteSmsTemplate";
            _request.AddQueryParameters("TemplateCode", templateCode);//模板Code
            return Json<AddAndDelDigitalTemResponse>(_request);
        }

        public QueryDigitalTemResponse QueryDigitalTemplate(string templateCode)
        {
            _request.Action = "QuerySmsTemplate";
            _request.AddQueryParameters("TemplateCode", templateCode);
            return Json<QueryDigitalTemResponse>(_request);
        }

        public QuerySendSmsDetails QueryDigitalSendDetails(string phoneNumber, string sendDate, int pageSize,
            int currentPage)
        {
            _request.Action = "QuerySendDetails";
            _request.AddQueryParameters("PhoneNumber", phoneNumber);
            _request.AddQueryParameters("SendDate", sendDate);
            _request.AddQueryParameters("PageSize", pageSize.ToString());
            _request.AddQueryParameters("CurrentPage", currentPage.ToString());
            return Json<QuerySendSmsDetails>(_request);
        }

        public SendSmsResponse SendDigitalSms(string phoneNumbers, string templateCode, Dictionary<string, string> templateParam)
        {
            _request.Action = "SendSms";
            _request.AddQueryParameters("SignName", "11111");//
            _request.AddQueryParameters("PhoneNumbers", phoneNumbers);
            _request.AddQueryParameters("TemplateCode", templateCode);
            _request.AddQueryParameters("TemplateParam", JsonSerializer.Serialize(templateParam));
            return Json<SendSmsResponse>(_request);
        }

        public SendSmsResponse SendDigitalSmsBatch(List<string> phoneNumbers, string templateCode,
            List<Dictionary<string, string>> templateParam)
        {
            _request.Action = "SendBatchSms";
            _request.AddQueryParameters("SignNameJson", JsonSerializer.Serialize(new List<string>() { "123", "123" }));
            _request.AddQueryParameters("PhoneNumberJson", JsonSerializer.Serialize(phoneNumbers));
            _request.AddQueryParameters("TemplateCode", templateCode);
            if (templateParam.Count != 0) _request.AddQueryParameters("TemplateParamJson", JsonSerializer.Serialize(templateParam));
            return Json<SendSmsResponse>(_request);
        }
    }
}
